package com.ztstudy.poi.Java_Map_demos.jihe.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

// 集合的遍历方式：迭代器
public class CollectionIterator {
    public static void main(String[] args) {

        Collection coll=new ArrayList();
        coll.add(new book("java", 100, "james"));
        coll.add(new book("phyton", 98, "james"));
        coll.add(new book("c++", 99, "james"));

        System.out.println("coll="+coll);

        // 使用迭代器遍历集合
        //1. 获取迭代器对象
        Iterator iterator = coll.iterator();
        //2.使用循环遍历，while快捷键=>itit+回车
        while (iterator.hasNext()) {//判断是否还有下一个元素
            //3.获取元素
            Object obj = iterator.next();//获取下一个元素，返回的是Object类型，取决于集合中存储的类型
            System.out.println(obj);
        }
        //3.当迭代器退出后，再使用迭代器，会报异常：NoSuchElementException
        // iterator.next();
        //如果需要重新使用，需要重新获取迭代器对象，或者重置迭代器对象
        //iterator = coll.iterator();


        //显示所有快捷键：ctrl+J ，ctrl+shift+a



    }
}



class book {
    private String name;
    private double price;
    private String author;

    public book(String name, double price, String author) {
        this.name = name;
        this.price = price;
        this.author = author;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", author='" + author + '\'' +
                '}';
    }
}